package com.itlong.cloud.controller.system;

import com.itlong.cloud.POJO.DTO.operate.DeadQueueGetPageDTO;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.project.operate.ITimeTaskDeadMessageQueueServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      死信消息管理控制器
 * </desc>
 *
 * @createDate 2017/11/29
 */
@RestController
@RequestMapping("/system/deadQueue")
public class OperateDeadQueueController extends BaseController{

    @Autowired
    ITimeTaskDeadMessageQueueServiceClient iTimeTaskDeadMessageQueueServiceClient;

    /**
     * <desc>
     *      获取分页列表
     * </desc>
     *
     * @param deadQueueGetPageDTO 查询参数DTO
     * @return messageVO
     * @author Jiaqi.X
     * @createDate 2017/11/29
     */
    @RequestMapping(path = "getPage",method = RequestMethod.POST)
    @ResponseBody
    public Object getRolePage(DeadQueueGetPageDTO deadQueueGetPageDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            deadQueueGetPageDTO= HttpProtocolUtil.parseRequestParamToDTO(DeadQueueGetPageDTO.class,request);
            Page<TimeTaskDeadMessageQueuePO> page=iTimeTaskDeadMessageQueueServiceClient.getPage(deadQueueGetPageDTO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        } catch (Exception e) {
            throw new DataAccessException("【运营_系统_死信getPage】获取分页列表失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      发送死信
     * </desc>
     *
     * @param timeTaskDeadMessageQueuePO 死信消息实体类
     * @return messageVO
     * @author Jiaqi.X
     * @createDate 2017/11/29
     */
    @LoggerInfo(operateDesc = LogDescConstant.SEND_DEAD_QUEUE_LOG_DESC)
    @RequestMapping(path = "sendDeadMessage",method = RequestMethod.POST)
    @ResponseBody
    public Object sendDeadMessage(TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            timeTaskDeadMessageQueuePO= HttpProtocolUtil.parseRequestParamToDTO(TimeTaskDeadMessageQueuePO.class,request);
            iTimeTaskDeadMessageQueueServiceClient.sendMessage(timeTaskDeadMessageQueuePO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营_系统_死信sendDeadMessage】发送死信失败",e);
        }
        return messageVO;
    }
}
